package utils.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * TimeUtils
 *
 * @author hqw 2015-8-5
 */
public class TimeUtils {

    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat DATE_FORMAT_MONTH = new SimpleDateFormat("MM-dd");
    public static final SimpleDateFormat DATE_FORMAT_SecondToMinute = new SimpleDateFormat("mm:ss");
    public static final SimpleDateFormat DATE_FORMAT_HOUR_Minute = new SimpleDateFormat("HH:mm");
    public static final SimpleDateFormat DATE_FORMAT_HOUR_Minute_Sec = new SimpleDateFormat("HH:mm:ss");
    public static final SimpleDateFormat DATE_FORMAT_DATE_D = new SimpleDateFormat("yyyy.MM.dd");

    private TimeUtils() {
        throw new AssertionError();
    }

    /**
     * long time to string
     *
     * @param timeInMillis
     * @param dateFormat
     * @return
     */
    public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(timeInMillis));
    }

    /**
     * long time to string
     *
     * @param timeInMillis
     * @param dateFormat
     * @return
     */
    public static String getTime(String timeInMillis, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(timeInMillis));
    }


    /**
     * long time to string, format is {@link #DEFAULT_DATE_FORMAT}
     *
     * @param timeInMillis
     * @return
     */
    public static String getTime(long timeInMillis) {
        return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
    }

    /**
     * long time to string, format is {@link #DEFAULT_DATE_FORMAT}
     *
     * @param timeInMillis
     * @return
     */
    public static String getTimeDate(long timeInMillis) {
        return getTime(timeInMillis, DATE_FORMAT_DATE);
    }

    /**
     * 获取 MM-DD
     *
     * @param timeInMillis
     * @return
     */
    public static String getTimeMonth(long timeInMillis) {
        return getTime(timeInMillis, DATE_FORMAT_MONTH);
    }

    public static String getTimeYear(long timeInMillis) {
        return getTime(timeInMillis, DATE_FORMAT_DATE);
    }

    public static String getTimeHourMinute(long timeInMillis) {
        return getTime(timeInMillis, DATE_FORMAT_HOUR_Minute);
    }

    /**
     * long time to string, format is {@link #DATE_FORMAT_SecondToMinute}
     *
     * @param timeInMillis
     * @return
     */
    public static String getTimeSecondToMinute(long timeInMillis) {
        return getTime(timeInMillis, DATE_FORMAT_SecondToMinute);
    }

    /**
     * get current time in milliseconds
     *
     * @return
     */
    public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }

    /**
     * get current time in milliseconds
     *
     * @return
     */
    public static String getCurrentTimeLong() {
        return String.valueOf(System.currentTimeMillis());
    }

    /**
     * get current time in milliseconds, format is {@link #DEFAULT_DATE_FORMAT}
     *
     * @return
     */
    public static String getCurrentTimeInString() {
        return getTime(getCurrentTimeInLong());
    }

    /**
     * get current time in milliseconds
     *
     * @return
     */
    public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
        return getTime(getCurrentTimeInLong(), dateFormat);
    }

    /**
     * @param time
     * @param dateFormat
     * @return
     */
    public static String getDateFormateTime(String time, SimpleDateFormat dateFormat) {
        if (time == null)
            return "";
        return dateFormat.format(time);
    }

    /**
     * @param time
     * @param dateFormat
     * @return
     */
    public static String getDateFormateTime(long time, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(time));
    }

    /**
     * get current time ��ȥ oldtime
     *
     * @return
     */
    public static int getNowTime2OldTime(String oldtime) {
        if (oldtime == null)
            return -1;
        try {
            Date oltime = DEFAULT_DATE_FORMAT.parse(oldtime);
            return (new Date().getDay() - oltime.getDay());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 检测当前时间 是否是今天
     *
     * @param millions
     * @return
     */
    public static boolean isCurrentDay(long millions) {
        Date index;

        index = new Date(millions);
        String curDate = DATE_FORMAT_DATE.format(index);

        index = new Date();
        String oldDate = DATE_FORMAT_DATE.format(index);

        if (curDate.equals(oldDate)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取时间的Long
     *
     * @param str millions 或Date类型
     * @return
     */
    public static long getLongTime(String str) {
        if (str.contains(":")) {
            return getTimeLong(str);
        }

        return getMillinsLong(str);
    }

    /**
     * 获取毫秒 Long
     *
     * @param str
     * @return
     */
    public static long getMillinsLong(String str) {
        return Long.parseLong(str);
    }

    /**
     * 获取Date 毫秒
     *
     * @param str
     * @return
     */
    public static long getTimeLong(String str) {
        Date date = new Date();
        try {
            date = DEFAULT_DATE_FORMAT.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }


    /**
     * 两个时间之间相差距离多少小时
     *
     * @param str1 时间参数 1：
     * @param str2 时间参数 2：
     * @return 相差天数
     */
    public static long getDistanceHours(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        Date one;
        Date two;
        long days = 0;
        long hour = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24);
            hour = (diff / (60 * 60 * 1000) - days * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days * 24 + hour;
    }

    /** 获取东八时区 */
    public static long getCurrentTime() {
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        Date date = Calendar.getInstance().getTime();
        return date.getTime();
    }


    /**
     * 将时间毫秒值转成hh:mm:ss,小时数可以超过24
     *
     * @param timeInMillis
     * @return
     */
    public static String getTimeStr(long timeInMillis) {
        StringBuffer time = new StringBuffer();
        int hour = (int) (timeInMillis / (60 * 60 * 1000));
        if (hour < 10) {
            time.append("0" + hour);
        } else {
            time.append(hour);
        }

        time.append(":");
        timeInMillis = timeInMillis - hour * 60 * 60 * 1000;
        int min = (int) (timeInMillis / (60 * 1000));
        if (min < 10) {
            time.append("0" + min);
        } else {
            time.append(min);
        }

        time.append(":");
        timeInMillis = timeInMillis - min * 60 * 1000;
        int sec = (int) (timeInMillis / 1000);
        if (sec < 10) {
            time.append("0" + sec);
        } else {
            time.append(sec);
        }
        return time.toString();
    }


}
